package com.example.demo.controller;/**
 * ClassName: SubscribeController
 * Package: com.example.demo.controller
 * Description:
 *
 * @Author 理理
 * @Create 2024/11/16 19:19
 * @Version 1.0
 */

import com.example.demo.entity.Repair;
import com.example.demo.entity.Subscribe;
import com.example.demo.entity.TimeQuantum;
import com.example.demo.entity.TimeSlotAvailability;
import com.example.demo.mapper.SubscribeMapper;
import com.example.demo.service.SubscribeService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *@title: SubscribeController
 *@Author: Tangerine
 *@Date: 2024/11/16 19:19
 *@version 1.0
 */
@RestController
@RequestMapping("/subscribe")
public class SubscribeController {
    @Autowired
    private SubscribeMapper subscribeMapper;
    @Autowired
    private SubscribeService subscribeService;
    @Autowired
    private UserService userService;

    //查询所有数据
    @GetMapping("/")
    public List<Subscribe> index(){
        return subscribeMapper.findAll();
    }

    //分页查询,返回总条数及当前需要页的数据
    @GetMapping("/page")
    public Map<String,Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        pageNum = (pageNum-1)*pageSize;
        List<Subscribe> data = subscribeService.selectPage(pageNum,pageSize);
        Integer total = subscribeMapper.selectTotal();
        Map<String,Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;
    }

    //新增或修改一条预约
    @PostMapping
    public Integer save(@RequestBody Subscribe subscribe){
        return subscribeService.save(subscribe);
    }
    //根据id删除一条预约
    @DeleteMapping("/{id}")
    public Integer deleteById(@PathVariable Integer id){
        return subscribeService.deleteById(id);
    }

    //处理一条预约信息
    @PostMapping("/edit")
    public Integer editCount(@RequestBody Subscribe subscribe){
        return subscribeService.checkS(subscribe);
    }

    //分类查询
    @GetMapping("/sort")
    public List<Subscribe> sortSelect(@RequestParam String type,@RequestParam String state){
        if(!type.equals("")){
            return subscribeService.typeSelect(type);
        }else{
            return subscribeService.stateSelect(state);
        }
    }

    //查询全部历史预约
    @GetMapping("/history")
    public List<Subscribe> findHistory(@RequestParam String studentNumber){
        return subscribeService.findHistory(studentNumber);
    }
    //分页查询历史预约
    @GetMapping("/hPage")
    public List<Subscribe> findHistoryPage(@RequestParam String studentNumber,@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        pageNum = (pageNum-1)*pageSize;
        return subscribeService.findHistoryPage(studentNumber,pageNum,pageSize);
    }

    //查询不同类型的所有数据
    @GetMapping("/type")
    public List<Subscribe> labTypeSelect(@RequestParam Integer labType){
        return subscribeService.labTypeSelect(labType);
    }
    //?查询不同类型的所有数据,分页显示
    @GetMapping("/tPage")
    public List<Subscribe> labTypeSelectPage(@RequestParam Integer labType,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        pageNum = (pageNum-1)*pageSize;
        return subscribeMapper.labTypeSelectPage(labType,pageNum,pageSize);
    }

    //管理员查询自己管理实验室的预约信息
    @GetMapping("/admin")
    public List<Subscribe> adminGet(@RequestParam Integer id){
        String name = userService.adminName(id);
        return subscribeService.getAdminClasses(name);
    }
}
